package net.base;

import com.opensymphony.xwork2.Action;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.log4j.Logger;

/**
 * @author: jiangxiangwei
 * @date: 2009-12-18
 */
public class BaseAction extends ToolsAction implements Action {
    private BaseDao dao = null;

    public BaseDao getDao() {
        return dao;
    }

    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    private Logger log = Logger.getLogger(this.getClass());

    /**
     * 插入数据库操作
     */
    public String insert() throws Exception {
        try {
            Map map = this.parserRequest(this.getRequest());
            log.info("执行sql+++++++++++++++++++" + map.get("SQLID"));
            Object i = dao.insert((String) map.get("SQLID"), map);
            this.setResult("00", "添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            this.setResult("99", "添加失败！\n" + e.toString());
            throw new Exception();
        }
        return null;
    }

    /**
     * 更新数据操作
     */
    public String update() throws Exception {
        try {
            Map map = this.parserRequest(this.getRequest());
            log.info("执行sql+++++++++++++++++++" + map.get("SQLID"));
            int i = dao.update((String) map.get("SQLID"), map);
            if (i != 0)
                this.setResult("00", "更新成功！");
            else
                this.setResult("99", "更新失败！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            this.setResult("99", "更新失败！\n" + e.toString());
            throw new Exception();
        }
        return null;
    }

    /**
     * 删除数据操作
     */
    public String delete() throws Exception {
        try {
            String[] id = this.getRequest().getParameter("IDS").split(",");
            String sqlid = this.getRequest().getParameter("SQLID");
            log.info("执行sql+++++++++++++++++++" + sqlid);
            if (id.equals("")) {
                this.setResult("01", "请选择要删除的记录！");
                return null;
            }
            int i = 0;
            for (int n = 0; n < id.length; n++) {
                i = dao.delete(sqlid, id[n]);
            }
            this.setResult("00", "删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            this.setResult("99", "删除失败！\n" + e.toString());
            throw new Exception();
        }
        return null;
    }

    /**
     * 查询所有数据，不带分页
     */
    public String selectAll() {
        try {
            Map map = this.parserRequest(this.getRequest());
            log.info("执行sql+++++++++++++++++++" + map.get("SQLID"));
            List list = dao.getList((String) map.get("SQLID"), map);
            if (getResult().equals("jsonHtml") || getResult().equals("jsonResult")) {
                model.put("HTML_PATH", map.get("HTML_PATH"));
                model.put("data", list);
            } else if (getResult().equalsIgnoreCase("success")) {
                this.setResult("00", list);
                return null;
            } else {
                model.put("data", list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (getResult().equalsIgnoreCase("success")) {
                this.setResult("99", e.toString());
            } else {
                code = -1;
                msg = e.getMessage();
                expMsg = getExceptionMessage(e);
            }
            log.error(e.toString());

        }
        return getResult();
    }

    /**
     * 查询数据库操作 带分页
     */
    public String select() {
        try {
            Map map = this.parserRequest(this.getRequest());
            log.info("执行sql+++++++++++++++++++" + map.get("SQLID"));

            int totalCount = 0;
            if (map.containsKey("TOTALCOUNT")) {
                totalCount = Integer.parseInt(String.valueOf(map
                        .get("TOTALCOUNT")));
            }
            if (totalCount <= 0) {
                totalCount = dao.getListCount(map.get("SQLID") + "_Count", map);
            }

            int curpage = 0;
            if (map.containsKey("CURPAGE"))
                curpage = Integer.parseInt(String.valueOf(map.get("CURPAGE")));
            int pagesize = Integer
                    .parseInt(String.valueOf(map.get("PAGESIZE")));
            int start = (curpage) * pagesize;
            int totalPage = totalCount % pagesize == 0 ? totalCount / pagesize
                    : totalCount / pagesize + 1;

            map.put("START", start);
            map.put("END", map.get("PAGESIZE"));
            List list = dao.getList((String) map.get("SQLID"), map);

            Map pMap = new HashMap();
            pMap.put("TOTALCOUNT", totalCount);
            pMap.put("CURPAGE", curpage);
            pMap.put("TOTALPAGE", totalPage);
            pMap.put("PAGESIZE", pagesize);
            PageList pList = new PageList();
            pList.setDataList(list);
            pList.setPageMap(pMap);

            if (getResult().equals("jsonHtml") || getResult().equals("jsonResult")) {
                model.put("HTML_PATH", map.get("HTML_PATH"));
                model.put("data", list);
                model.put("pages", pMap);
            } else if (getResult().equalsIgnoreCase("success")) {
                this.setResult("00", pList);
                return null;
            } else {
                model.put("data", list);
                model.put("pages", pMap);
            }


        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            if (getResult().equalsIgnoreCase("success")) {
                this.setResult("99", e.toString());
            } else {
                code = -1;
                msg = e.getMessage();
                expMsg = getExceptionMessage(e);
            }
        }
        return getResult();
    }

    /**
     * 取单条数据操作
     */
    public String selectByID() {
        try {
            Map map = this.parserRequest(this.getRequest());
            log.info("执行sql+++++++++++++++++++" + map.get("SQLID"));
            Map rtnmap = dao.getMap((String) map.get("SQLID"), map);
            this.setResult("00", rtnmap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            this.setResult("99", e.toString());
        }
        return null;
    }
}
